package com.bulemobi.activity.ac_user_centre;

import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.*;
import com.google.gson.Gson;
import com.bulemobi.adapter.PopItemAdapter;
import com.bulemobi.bean.BankCarBean;
import com.bulemobi.data.DataCentre;
import com.bulemobi.net.tsz.afinal.FinalHttp;
import com.bulemobi.net.tsz.afinal.annotation.view.ViewInject;
import com.bulemobi.net.tsz.afinal.http.AjaxCallBack;
import com.bulemobi.net.tsz.afinal.http.AjaxParams;
import com.bulemobi.urlCentre.UrlCentre;
import com.bulemobi.view.BaseActivity;

import android.os.Bundle;
import com.bulemobi.view.titleView.ActionBarView;
import com.jsyh.hcb.R;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

public class AcBundlingCard extends BaseActivity implements View.OnClickListener {
    @ViewInject(id = R.id.actionBar)
    ActionBarView actionBarView;

    @ViewInject(id = R.id.ed_userName)
    EditText ed_userName;//姓名

    @ViewInject(id = R.id.ed_userNum)
    EditText ed_userNum;//身份证号码

    @ViewInject(id = R.id.tv_bankCard)
    TextView tv_bankCard;//选择的银行

    @ViewInject(id = R.id.ed_bankCardNum)
    EditText ed_bankCardNum;//银行的卡号

    @ViewInject(id = R.id.ed_phone)
    EditText ed_phone;//手机号

    @ViewInject(id = R.id.ed_ver)
    EditText ed_ver;//短信验证号

    @ViewInject(id = R.id.tv_give_verification)
    TextView tv_give_verification;//得到验证码

    @ViewInject(id = R.id.tv_confirm)
    TextView tv_confirm;//确认添加

    @ViewInject(id = R.id.tv_select)
    TextView tv_select;

    private final String INIT = "init";

    private PopupWindow popupWindow;

    private ArrayList<BankCarBean> bankList = new ArrayList<BankCarBean>();

    private String bankNo;

    private ListView lv_pop;

    private PopItemAdapter adapter;

    private boolean isSelect = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ac_bundling_card);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN
                | WindowManager.LayoutParams.SOFT_INPUT_ADJUST_NOTHING);
        initView();
        //获取开户行列表
        qryCard();
    }

    private void qryCard() {
        AjaxParams params = new AjaxParams();
        params.put("ChannelId", UrlCentre.ChannelId);
        params.put("UserType", UrlCentre.UserType);
        params.put("TerminalType", UrlCentre.TerminalType);
        requestHttp(UrlCentre.BankListQry, params, INIT);
    }

    private void requestHttp(String url, AjaxParams params, final String type) {
        FinalHttp http = new FinalHttp();
        http.post(url, params, new AjaxCallBack<Object>() {

            @Override
            public void onStart() {
                showProgressDialog();//开启旋转的
                // TODO Auto-generated method stub
                super.onStart();
            }

            @Override
            public void onSuccess(Object t) {
                closeProgressDialog();//关闭旋转的
                try {
                    JSONObject object = new JSONObject(t.toString());
                    if (type.equals(INIT)) {
                        //获取数据成功的状态下
                        if (object.getString("_RejCode").equals(DataCentre.RIGHT_RejCode)) {
                            JSONArray array = object.getJSONArray("BankList");
                            for (int i = 0; i < array.length(); i++) {
                                BankCarBean bean = new Gson().fromJson(array.getJSONObject(i).toString(), BankCarBean.class);
                                bankList.add(bean);
                            }
                            initPop();
                        }
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }

                Log.d("OnSuccess", t.toString());
                // TODO Auto-generated method stub
                super.onSuccess(t);
            }

            @Override
            public void onFailure(Throwable t, int errorNo, String strMsg) {
                // TODO Auto-generated method stub
                closeProgressDialog();//关闭旋转的
                super.onFailure(t, errorNo, strMsg);
            }

        });
    }

    /**
     * 初始化popupwindow
     */
    private void initPop() {
        View view = LayoutInflater.from(this).inflate(R.layout.pop_item, null);
        lv_pop = (ListView) view.findViewById(R.id.lv_pop);
        popupWindow = new PopupWindow(view, 200, 300);
        popupWindow.setFocusable(true);
        popupWindow.setWidth(tv_bankCard.getWidth());
        lv_pop.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                tv_bankCard.setText(bankList.get(position).getBankName());
                bankNo = bankList.get(position).getBankNo();
                popupWindow.dismiss();
            }
        });
        initPopData();
    }

    /**
     * 初始化pop数据
     */
    private void initPopData() {
        adapter = new PopItemAdapter(this, bankList);
        lv_pop.setAdapter(adapter);
    }

    private boolean checkLogin() {
        if (TextUtils.isEmpty(DataCentre.sessionId)) {
            return false;
        } else {
            return true;
        }
    }

    private void initView() {
        actionBarView.title.setText(getResources().getString(R.string.ac_bundling_card_title));
        tv_confirm.setOnClickListener(this);
        tv_give_verification.setOnClickListener(this);
        tv_bankCard.setOnClickListener(this);
        tv_select.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        if (view.getId() == tv_confirm.getId()) {
            if (ed_userName.getText().length() <= 0) {
                showToastShort(getResources().getString(R.string.error_usrnamese));
                return;
            } else if (ed_userNum.getText().length() < 18) {
                showToastShort(getResources().getString(R.string.error_proof_of_identity));
                return;
            } else if (ed_bankCardNum.getText().length() < 19) {
                showToastShort(getResources().getString(R.string.error_bank_card));
                return;
            } else if (ed_phone.getText().length() < 11) {
                showToastShort(getResources().getString(R.string.error_phones));
                return;
            } else if (ed_ver.getText().length() <= 0) {
                showToastShort(getResources().getString(R.string.error_notes));
                return;
            }
            if (!isSelect) {
                return;
            }
            AjaxParams params = new AjaxParams();
//            params.put("CifName", ed_userName.getText().toString());//证件姓名
            params.put("CifName", "王云秋");//证件姓名

//            params.put("IdNo", ed_userNum.getText().toString());//身份证号
            params.put("IdNo", "6228760005000009159");//身份证号

            params.put("IsDefaultCard", "0");//选择开户银行 0-否 1-是（默认传0）

//            params.put("BankNo",bankNo);//银行卡分类
            params.put("BankNo", "32000014010001");//银行卡分类

//            params.put("bankCardNo", ed_bankCardNum.getText().toString());//银行卡号
            params.put("bankCardNo", "6228760005000009159");//银行卡号

//            params.put("mobile", ed_phone.getText().toString());//手机号
            params.put("mobile", "13675178888");//手机号

            //短信验证码
            params.put("checkCode", ed_ver.getText().toString());//短信验证码

            params.put("TerminalType", UrlCentre.TerminalType);
            params.put("UserType", UrlCentre.UserType);
            params.put("ChannelId", UrlCentre.ChannelId);

            requestHttp(UrlCentre.CarRelatedAcAdd, params, "");

        } else if (view.getId() == tv_give_verification.getId()) {//短信验证码
            AjaxParams params = new AjaxParams();
            params.put("Type", DataCentre.FINDPASS_TYPE);
            params.put("CheckType", DataCentre.BOUND_BANK_CARD);
            params.put("TerminalType", UrlCentre.TerminalType);
            params.put("UserType", UrlCentre.UserType);
            params.put("ChannelId", UrlCentre.ChannelId);

            requestHttp(UrlCentre.GetSMSToken, params, "");
        } else if (view.getId() == tv_bankCard.getId()) {
            popupWindow.showAsDropDown(tv_bankCard);
        } else if (view.getId() == tv_select.getId()) {
            tv_select.setBackgroundResource(R.drawable.register_unselect);
            isSelect = false;
        }
    }
}
